<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    /**
     * Constructor de la clase makinita.
     * @param array $entrada
     * @param array $reglas
     * @return boolean $error
     */

     
class sfTableOrderBy {
    
    private $name;
    private $text;
    private $style;
    private $htmlClass;
    private $parent;
        
    private $query;
    
    private $orderCol = null;
    private $orderDir = null;

    private $selectList = array();

    public function __construct($style = null ,$htmlClass = null ) {

        //$this->name = $name;
        $this->style = $style;
        $this->htmlClass = $htmlClass;        
        
    }

    public function bind($parameters) {
        
        if(isset($parameters['orderCol']))
            $this->orderCol = $parameters['orderCol'];
        else
            $this->orderCol = $this->parent->thSchema->getDefaultSort();
        

        if(isset($parameters['orderDir']))
            $this->orderDir = $parameters['orderDir'];
        else 
            $this->orderDir = $this->parent->thSchema->getDefaultSortDirection();            
        
    }
    
    
    public function setQuery($query) {
        $this->query = $query;
    }
    
    public function setParent($parent) {
        $this->parent = $parent;
    }

    
    public function execute() {
        
        foreach($this->parent->thSchema as $k => $th)
        {
            if($th->isSortable())
            {
                //cargo la lista para el render del select
                
                $this->selectList[$k]['text'] = $th->getText();
                $this->selectList[$k]['selected'] = 0;
                
                //echo 'sortable-';
                if($k == $this->orderCol)
                {
                    //marco que esta es la elegida
                    $this->selectList[$k]['selected'] = 1;                    
                    
                    if($this->orderCol != null)
                    {
                        $this->selectList[$k]['dir'] = $this->orderDir;
                    }
                    else
                    {
                        $this->selectList[$k]['dir'] = $th->getSortDirection();
                    }
                    
                    //$field = $this->parent->tdSchema[$k]->getField();
                    
                    $alias = $this->parent->getAlias($this->parent->tdSchema[$k]->getField());
                    $leaf = $this->parent->tdSchema[$k]->getOption('leaf');

                    
                    $this->query->addOrderBy($alias.'.'.$leaf.' '.$this->selectList[$k]['dir']);
                                        
//                    $orderItem = substr($field,strrpos($field, "."));
//                    
//                    //Esto de aca es para diferenciar campos del raiz de 
//                    //rutas completas con relaciones
//                    
//                    if(substr($orderItem[0],0,1) == '.')
//                    {
//                        $this->query->addOrderBy($this->parent->aliasTranslate[$k].$orderItem.' '.$this->selectList[$k]['dir']);//.$orderItem);  
//                    }  else {
//                        $this->query->addOrderBy($orderItem.' '.$this->selectList[$k]['dir']);
//                    }
                    
                }

            }
        }

        return $this->query;
    }
    
    public function getName() {
        return $this->name;
    }

    public function render() {

        $selectName = $this->parent->getName().'Values[orderCol]';
        $directionName = $this->parent->getName().'Values[orderDir]';
        
        $selectedField['dir'] = '';
        
        
        $result = '<tr style = "'.$this->style.'" class = "'.$this->htmlClass.'" >';
        
        $result .= '<td colspan="0" style="padding:5px"> Ordenar por <select id="'.$selectName.'" name="'.$selectName.'">'.chr(10);
        
        
        foreach($this->selectList as $k => $field)
        {
                if($field['selected'] == 1)
                {
                    $result .= '<option selected = "selected" ';
                    $selectedField = $field;
                }  else {
                    $result .= '<option ';                        
                }

                $result .= 'value="'.$k.'">'.$field['text'].'</option>'.chr(10);
        }


        $result .= '</select> '.chr(10);    

        $result .= '<select id="'.$directionName.'" name="'.$directionName.'">'.chr(10);            


            $result .= '<option value = "asc"'; 
                if($selectedField['dir'] == 'asc')$result .='selected = "selected"';                
            $result .= '> Asc </option>'.chr(10);

            $result .= '<option value = "desc"'; 
                if($selectedField['dir'] == 'desc')$result .='selected = "selected"';                
            $result .= '> Desc </option>'.chr(10);

        $result .= '</select>'.chr(10);         

        
        $result .= '<input type="submit" value="Ordenar" class="tableButtonSimple">'.chr(10);

        $result .= '</td>'.chr(10).'</tr>'.chr(10);
        
        return $result;
        
    }
    
}


?>
